//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_review_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_review_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type ReviewResultItemDbItem struct {
	ResultId  string `bson:"_id"`
	ProjectId string `bson:"projectId"`
	ReviewId  string `bson:"reviewId"`
	Content   string `bson:"content"`
	TimeStamp int64  `bson:"timeStamp"`
}

func (item *ReviewResultItemDbItem) ToReviewResultItem() *project_review_api.ReviewResultItem {
	return &project_review_api.ReviewResultItem{
		ResultId:  item.ResultId,
		Content:   item.Content,
		TimeStamp: item.TimeStamp,
	}
}

type _ResultDao struct {
	mc *mongo.Collection
}

func newResultDao(db *mongo.Database) (*_ResultDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_REVIEW + "result")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"reviewId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_ResultDao{mc: mc}, nil
}

func (db *_ResultDao) Insert(ctx context.Context, dbItem *ReviewResultItemDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_ResultDao) Exist(ctx context.Context, projectId, reviewId, resultId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": resultId, "projectId": projectId, "reviewId": reviewId})
	return count > 0, err
}

func (db *_ResultDao) Get(ctx context.Context, projectId, reviewId, resultId string) (*ReviewResultItemDbItem, error) {
	retItem := &ReviewResultItemDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": resultId, "projectId": projectId, "reviewId": reviewId}).Decode(retItem)
	return retItem, err
}

func (db *_ResultDao) Remove(ctx context.Context, projectId, reviewId, resultId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": resultId, "projectId": projectId, "reviewId": reviewId})
	return err
}

func (db *_ResultDao) RemoveByReview(ctx context.Context, projectId, reviewId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "reviewId": reviewId})
	return err
}

func (db *_ResultDao) Update(ctx context.Context, projectId, reviewId, resultId, content string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": resultId, "projectId": projectId, "reviewId": reviewId},
		bson.M{"$set": bson.M{
			"content": content,
		}})
	return err
}

func (db *_ResultDao) List(ctx context.Context, projectId string, reviewIdList []string) ([]*ReviewResultItemDbItem, error) {
	if len(reviewIdList) == 0 {
		return []*ReviewResultItemDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId, "reviewId": bson.M{"$in": reviewIdList}}, options.Find().SetSort(bson.M{"timeStamp": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*ReviewResultItemDbItem{}

	err = cursor.All(ctx, &retList)
	return retList, err
}
